$(function () {
    var school_tab = $('#school_tab');
    var school_edit = $("#school_edit");
    var school_dialog = $("#school_dialog");
    var trackSchool_dialog = $("#trackSchool_dialog");
    var speech_dialog = $("#speech_dialog");
    var trackSchool_form = $("#trackSchool_form");
    var speect_form = $("#speect_form");
    var school_roles = $("#school_roles");
    var speech_list_dialog = $("#speech_list_dialog");
    var speech_list = $("#speech_list");
    school_tab.datagrid({
        url:'/school/list',
        fit:true,
        fitColumns:true,
        border:false,
        rownumbers:true,
        pagination:true,
        singleSelect:true,
        toolbar:'#school_tab_tool',
        columns:[[
            {field:'id',title:'编号',width:50},
            {field:'name',title:'学校',width:100},
            {field:'shc',title:'学校类型',formatter:formatterWord},
            {field:'address',title:'地址',width:100},
            {field:'improtance',title:'重要度',width:100,formatter:formatterWord},
            {field:'wash',title:'意向度',width:100,formatter:formatterWord},
            {field:'tel',title:'电话',width:100},
            {field:'washClass',title:'意向学科',width:100,formatter:formatterWord},
            {field:'linkman',title:'学校联系人',width:100,formatter:formatterLinkman},
            {field:'sale',title:'销售人员',width:100,formatter:formatterschool},
            {field:'currentSale',title:'跟进人员',width:100,formatter:formatterschool},
            {field:'clientTotal',title:'潜在学员数',width:100},
            //{field:'workTotal',title:'实训次数',width:100,formatter:formatterschool},
            {field:'speechTotal',title:'演讲次数',width:100},
            {field:'regStu',title:'正式学员数',width:100},
            {field:'prevTime',title:'上次跟踪时间',width:100},
            {field:'nextTime',title:'下次跟踪时间',width:100},
            {field:'followState',title:'跟踪状态',formatter:formatterFollowState},
            {field:'hands',title:'是否合作',width:100,formatter:formatterHands},
            {field:'state',title:'签约',width:100,formatter:formatterState},
        ]],
        onSelect:function (index, row) {
            //判断签约和合作
            if (row.state){
                //未签约----设置为签约
                $("#singing").linkbutton({"text":"签约"});
            }else {
                //设置为取消签约
                $("#singing").linkbutton({"text":"取消签约"});
            }
            if (row.hands){
                //合作状态----设置为取消合作
                $("#cooperation").linkbutton({"text":"取消合作"});
            }else {
                $("#cooperation").linkbutton({"text":"合作"});
            }
        },
        onDblClickRow:function (index, row){
            var id = row.id;
            var url = "/school/getSpeechListBySchoolId?schoolId=" + id;
            speech_list.datagrid({
                url:url,
                fit:true,
                fitColumns:true,
                border:false,
                rownumbers:true,
                singleSelect:true,
                columns:[[
                    {field:'id',title:'编号',width:50},
                    {field:'speechTime',title:'时间',width:100},
                    {field:'address',title:'演讲地址'},
                    {field:'school',title:'演讲学校',formatter:formatterAddress},
                    {field:'speecher',title:'演讲人',formatter:formatterSpeecher},
                    {field:'result',title:'演讲效果',width:100},
                    {field:'remark',title:'备注',width:100},
                ]]
            });
            //格式化信息
            function formatterAddress(value,row,index) {
                console.log(value);
                return value ? value.name:"";
            }
            function formatterSpeecher(value,row,index) {
                console.log(value);
                return value ? value.realName:"";
            }
            //打开窗口
            objMethod.openSpeech()
        }
    });
    //格式化信息
    function formatterWord(value,row,index) {
        return value ? value.name:"";
    }
    function formatterLinkman(value,row,index) {
        return value ? value.name:"";
    }
    //
    function formatterschool(value,row,index) {
        return value ? value.realName:"";
    }
    //合作状态
    function formatterHands(value,row,index) {
        return !value ? "<span style='color: red'>否</span>":"<span style='color: green'>是</span>";
    }
    //签约状态
    function formatterState(value,row,index) {
        return !value ? "<span style='color: red'>未签约</span>":"<span style='color: green'>已签约</span>";
    }
    function formatterFollowState(value,row,index) {
        if(value){
            if(value == 1){
                return "<span style='color: green'>已跟踪</span>";
            }
            return "<span style='color: red'>丢单</span>";
        }
        return  "<span style='color: red'>待跟踪</span>";
    }

    var objMethod = {
        //刷新页面
        reloadList:function () {
            school_tab.datagrid("reload");
        },
        save:function () {
            var url = "/school/save";
            //获取员工id
            var schoolId = $("#school_id").val();
            if (schoolId){
                url = "/school/update"
            }
            //提交表单
            school_edit.form("submit",{
                url:url,
                //表单提交之前
                onSubmit:function (param) {
                    //获取所有选中的
                    var man = $("#school_linkman").val();
                    //设置参数---如果设置的联系人不是一个数则设置name为linkman.name
                    if(isNaN($("#school_linkman").val())){
                        $("#school_linkman").prop("name","linkman.name")
                    }else {
                        $("#school_linkman").prop("name","linkman.id")
                    }
                },
                success:function (data) {
                    data = $.parseJSON(data);
                    if(data.success){
                        $.messager.alert("提示","操作成功","info",function () {
                            //关闭窗口刷新页面
                            objMethod.cancel();
                            objMethod.reloadList();
                        });
                    }else {
                        $.messager.alert("提示",data.msg,"error");
                    }
                }
            });
        },
        //跟踪客户
        track:function () {
            var url = "/school/trackSchool";
            //提交表单
            trackSchool_form.form("submit",{
                url:url,
                //表单提交之前
                /*onSubmit:function (param) {
                    //获取所有选中的角色
                    var selectedRoles = school_roles.combobox("getValues");
                    //遍历,设置参数
                    for(var i = 0; i < selectedRoles.length; i ++){
                        param['roles['+i+'].id'] = selectedRoles[i];
                    }
                },*/
                success:function (data) {
                    data = $.parseJSON(data);
                    if(data.success){
                        $.messager.alert("提示","设置跟踪成功","info",function () {
                            //关闭窗口刷新页面
                            objMethod.cancel();
                            objMethod.reloadList();
                        });
                    }else {
                        $.messager.alert("提示",data.msg,"error");
                    }
                }
            });
        },
        //关闭窗口
        cancel:function () {
            school_dialog.dialog("close");
            trackSchool_dialog.dialog("close");
            speech_dialog.dialog("close");
        },
        //打开编辑窗口
        addschool:function () {
            //清空表中数据
            school_edit.form("clear");
            //重新加载联系人  /schoolLinkman/selectAll
            $("#school_linkman").combobox("reload","/schoolLinkman/selectAll");
            school_dialog.dialog("setTitle","编辑");
            school_dialog.dialog("open");
        },
        //打开演讲列表窗口
        openSpeech:function () {
            //清空表中数据
            speech_list_dialog.dialog("open");
        },
        //打开编辑演讲窗口
        addSpeech: function () {

            //清空表中数据
            speect_form.form("clear");

            //获取选中的行
            var selectedRow = school_tab.datagrid("getSelected");
            //判断是否选中行
            if (!selectedRow) {
                $.messager.confirm("提示", "没有选中行:表示自定义演讲地点", function (yes) {
                    if (yes) {
                        speech_dialog.dialog("setTitle", "添加自定义演讲地点");
                        speech_dialog.dialog("open");
                    }
                });
            } else {
                speech_dialog.dialog("setTitle", "添加[" + selectedRow.name + "]学校的演讲");
                //设置school.id
                $("#speech_school_school").val(selectedRow.id);
                speech_dialog.dialog("open");
            }
        },
        //保存演讲
        speechSave:function () {
            var url = "/school/speechSave";
            //提交表单
            speect_form.form("submit",{
                url:url,
                success:function (data) {
                    data = $.parseJSON(data);
                    if(data.success){
                        $.messager.alert("提示","操作成功","info",function () {
                            //关闭窗口刷新页面
                            objMethod.cancel();
                            objMethod.reloadList();
                        });
                    }else {
                        $.messager.alert("提示",data.msg,"error");
                    }
                }
            });
        },
        //打开跟踪窗口
        trackSchool:function () {
            //获取选中的行
            var selectedRow = school_tab.datagrid("getSelected");
            //判断是否选中行
            if(!selectedRow){
                $.messager.alert("提示","请选中一行数据","error");
                return;
            }
            //清空表中数据
            trackSchool_form.form("clear");

            //关联对象回显
            if(selectedRow.currentSale){
                selectedRow['currentSale.id'] = selectedRow.currentSale.id;
            }

            trackSchool_form.form("load",selectedRow);

            trackSchool_dialog.dialog("open");
        },
        //丢单
        deleteschool:function () {
            //获取选中的行
            var selectedRow = school_tab.datagrid("getSelected");
            //判断是否选中行
            if(!selectedRow){
                $.messager.alert("提示","请选中一行数据","error");
                return;
            }
            $.messager.confirm("提示","是否确定丢单",function (yes) {
               if (yes){
                   $.post("/school/delete",{id:selectedRow.id},function (data) {
                      if(data.success){
                          $.messager.alert("提示","操作成功","info",function () {
                              //刷新页面
                              objMethod.reloadList();
                          });
                      } else {
                          $.messager.alert("提示",data.msg,"error");
                      }
                   });
               }
            });
        },
        //签约
        signing:function () {
            //获取选中的行
            var selectedRow = school_tab.datagrid("getSelected");
            //判断是否选中行
            if(!selectedRow){
                $.messager.alert("提示","请选中一行数据","error");
                return;
            }
            $.messager.confirm("提示","是否确定签约",function (yes) {
               if (yes){
                   $.post("/school/signing",{id:selectedRow.id},function (data) {
                      if(data.success){
                          $.messager.alert("提示","操作成功","info",function () {
                              //刷新页面
                              objMethod.reloadList();
                          });
                      } else {
                          $.messager.alert("提示",data.msg,"error");
                      }
                   });
               }
            });
        },
        //合作
        cooperation:function () {
            //获取选中的行
            var selectedRow = school_tab.datagrid("getSelected");
            //判断是否选中行
            if(!selectedRow){
                $.messager.alert("提示","请选中一行数据","error");
                return;
            }
            $.messager.confirm("提示","是否确定合作",function (yes) {
               if (yes){
                   $.post("/school/cooperation",{id:selectedRow.id},function (data) {
                      if(data.success){
                          $.messager.alert("提示","操作成功","info",function () {
                              //刷新页面
                              objMethod.reloadList();
                          });
                      } else {
                          $.messager.alert("提示",data.msg,"error");
                      }
                   });
               }
            });
        },
        //编辑
        editschool:function () {
            //获取选中的行
            var selectedRow = school_tab.datagrid("getSelected");
            console.log(selectedRow);
            //判断是否选中行
            if(!selectedRow){
                $.messager.alert("提示","请选中一行数据","error");
                return;
            }
            //设置表单回显--清空表中数据
            school_edit.form("clear");

            //设置关联对象回显---
            if(selectedRow.improtance){
                selectedRow['improtance.id'] = selectedRow.improtance.id;
            }
            if(selectedRow.linkman){
                selectedRow['wash.id'] = selectedRow.wash.id;
            }
            if(selectedRow.shc){
                selectedRow['shc.id'] = selectedRow.shc.id;
            }
            //设置参数---如果设置的联系人不是一个数则设置name为linkman.name

            if(selectedRow.linkman){
                $("#school_linkman").combobox("setValue",selectedRow.linkman.id);
            }
            if(selectedRow.washClass){
                selectedRow['washClass.id'] = selectedRow.washClass.id;
            }
            if(selectedRow.schAddress){
                selectedRow['schAddress.id'] = selectedRow.schAddress.id;
            }

            /*//角色回显
            $.post("/role/getRoleIdsByschoolId",{schoolId:selectedRow.id},function (data) {
                school_roles.combobox("setValue",data);
            });*/
            //console.log(selectedRow);

            //school_edit.form("load","/school/getSchoolById?id=" + selectedRow.id);
            $("#school_linkman").combobox("reload","/schoolLinkman/getLinkmanBySchoolId?schoolId=" + selectedRow.id);
            school_edit.form("load",selectedRow);
            school_dialog.dialog("setTitle","编辑");
            school_dialog.dialog("open");
        },

        //高级查询
        queryForList:function () {
            var keyword = $("#keyword").val();
            var washClass =$("#washClass").val();
            school_tab.datagrid("load",{keyword:keyword,washClass:washClass});
        },
        downloadList:function () {
            window.location.href="/school/download";
        }

    };
    //统一绑定事件
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        objMethod[cmd]();
    });
    //新增员工弹框
    school_dialog.dialog({
        title: '编辑',
        closed: true,
        buttons:'#school_edit_tool'
    });
    //设置跟单弹框
    trackSchool_dialog.dialog({
        title: '跟单',
        closed: true,
        buttons:'#trackSchool_dialog_tool'
    });
    //当前学校演讲列表
    speech_list_dialog.dialog({
        title: '演讲列表',
        width:600,
        height:300,
        closed: true
    });



    //添加演讲弹框
    speech_dialog.dialog({
        title: '演讲',
        closed: true,
        buttons:'#speech_dialog_tool'
    });
});
